import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { addBuilder } from "store/dict";
import * as services from "services/login";
import { RegisterData, forget } from "services/login";

//完善邮箱
export const improve = createAsyncThunk<any, any>(
  "loginstate/improve",
  async (data) => {
    const res = services.improve(data.data, data.email);
    return res;
  }
);

//设置新密码
export const newpwd = createAsyncThunk<any, any>(
  "loginstate/newpwd",
  async (data) => {
    const res = services.newpwd(data);
    return res;
  }
);

//获取验证码
export const logindata = createAsyncThunk<any, string>(
  "Login/logindata",
  async (email) => {
    const res = services.logindata(email);
    console.log(res);

    return res;
  }
);

//注册
export const regiser = createAsyncThunk<any, RegisterData>(
  "Login/regiser",
  async (data) => {
    const res = services.doRegister(data);
    return res;
  }
);

//登录
export const logining = createAsyncThunk<any, any>(
  "Login/logining",
  async (data) => {
    const res = services.logining(data);
    return res;
  }
);

//忘记密码
export const forgetpwd = createAsyncThunk<any, forget>(
  "loginstate/forgetpwd",
  async (data) => {
    const res = services.forgetpwd(data);
    return res;
  }
);

const initialState = {
  loginlist: {},
  uesrInfo: null,
};

const loginstate = createSlice({
  name: "loginstate",
  initialState,
  reducers: {},
  extraReducers: (buildr) => {
    addBuilder(buildr, logindata, (state, action) => {
      // console.log(action);
    });
    addBuilder(buildr, regiser, (state, action) => {
      // console.log(action);
    });
    addBuilder(buildr, logining, (state, action) => {
      console.log(action);
      if (action.error?.message) {
        return;
      }
      state.uesrInfo = action.payload.result;
    });
    addBuilder(buildr, forgetpwd, (state, action) => {
      // console.log(action);
    });
    addBuilder(buildr, newpwd, (state, action) => {
      // console.log(action);
    });
    addBuilder(buildr, improve, (state, action) => {
      // console.log(action);
    });
  },
});

export default loginstate.reducer;

// export const userSelector = (state: any) => state.loginstate.uesrInfo;
